package com.cssl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cssl.domain.DtsAdmin;
import com.cssl.domain.DtsRole;
import com.cssl.domain.dto.DtsAdDto;
import com.cssl.domain.dto.DtsRoleDto;
import com.cssl.domain.vo.PermVo;
import com.cssl.service.DtsAdService;
import com.cssl.service.DtsAdminService;
import com.cssl.service.DtsPermissionService;
import com.cssl.service.DtsRoleService;
import com.cssl.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/role")
public class DtsRoleController {
    @Autowired
    private DtsRoleService roleService;
    @Autowired
    private DtsPermissionService permissionService;
    @Autowired
    private DtsAdminService dtsAdminService;

    @PostMapping("/roleOptions")
    public Result roleOptions() {
        List<DtsRole> roleList = roleService.queryAll();
        List<Map<String, Object>> options = new ArrayList<>(roleList.size());
        for (DtsRole role : roleList) {
            Map<String, Object> option = new HashMap<>(2);
            option.put("value", role.getId());
            option.put("label", role.getName());
            options.add(option);
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, options, System.currentTimeMillis());

    }

    @PostMapping("/listRole")
    public Result getlistRole(@RequestBody DtsRoleDto dto) {
        Map roleList = roleService.getDtsRoleList(dto);
        if (ObjectUtil.isEmpty(roleList.get("list"))) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, roleList, System.currentTimeMillis());

    }

    @PostMapping("/createRole")
    public Result createRole(@RequestBody DtsRole role) {
        int i = roleService.RoleNameCount(role.getName());

        if (i > 0) {
            return Result.result(InfoCode.FAILED, "角色名已存在", null, System.currentTimeMillis());
        } else {
            roleService.createRole(role);
            return Result.result(InfoCode.CREATED, InfoCode.MESSAGE_CREATED, null, System.currentTimeMillis());
        }

    }

    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody DtsRole role) {
        int i = roleService.updateRole(role);
        if (i > 0) {
            return Result.result(InfoCode.OK, "", null, System.currentTimeMillis());
        } else {
            return Result.result(InfoCode.FAILED, "", null, System.currentTimeMillis());
        }

    }

    @PostMapping("/deleteRole")
    public Result deleteRole(@RequestBody DtsRole role) {
        if (role.getId() != null && !role.equals("0")) {
            if (role.getId()==1){
                return Result.result(InfoCode.FAILED, "超级管理员无法删除", null, System.currentTimeMillis());
            }
            int deleteRole = roleService.deleteRole(role);
             permissionService.deletePermission(role);
             dtsAdminService.updateRoleAdmin(role);
            if (deleteRole > 0) {
                return Result.result(InfoCode.NO_CONTENT, InfoCode.MESSAGE_NO_CONTENT, null, System.currentTimeMillis());
            } else {
                return Result.result(InfoCode.FAILED, "删除失败", null, System.currentTimeMillis());
            }

        }
        return Result.result(InfoCode.NOT_FOUND, InfoCode.MESSAGE_NOT_FOUND, null, System.currentTimeMillis());
    }

    @Autowired
    private ApplicationContext context;
    private List<PermVo> systemPermissions = null;
    private Set<String> systemPermissionsString = null;

    private List<PermVo> getSystemPermissions() {
        final String basicPackage = "com.cssl.domain";
        if (systemPermissions == null) {
            List<Permission> permissions = PermissionUtil.listPermission(context, basicPackage);
            systemPermissions = PermissionUtil.listPermVo(permissions);
            systemPermissionsString = PermissionUtil.listPermissionString(permissions);
        }
        return systemPermissions;
    }

    private Set<String> getAssignedPermissions(Integer roleId) {
        // 这里需要注意的是，如果存在超级权限*，那么这里需要转化成当前所有系统权限。
        // 之所以这么做，是因为前端不能识别超级权限，所以这里需要转换一下。
        Set<String> assignedPermissions = null;
        if (permissionService.checkSuperPermission(roleId)) {
            getSystemPermissions();
            assignedPermissions = systemPermissionsString;
        } else {
            assignedPermissions = permissionService.queryByRoleId(roleId);
        }

        return assignedPermissions;
    }

    @PostMapping("/permissions")
    public Result permissions(@RequestBody DtsRole role) {

        List<PermVo> systemPermissions = getSystemPermissions();
        Set<String> assignedPermissions = getAssignedPermissions(role.getId());
        Map<String, Object> data = new HashMap<>();
        data.put("systemPermissions", systemPermissions);
        data.put("assignedPermissions", assignedPermissions);
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, data, System.currentTimeMillis());
    }
}
